<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 商城订单管理
 *
 * @icon fa fa-circle-o
 */
class OrderNum extends Backend
{

    protected $noNeedRight = ['export'];

    /**
     * Order模型对象
     * @var \app\common\model\shop\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\shop\Order;
    }

    /**
     * 数据统计
     */
    public function index(){
        //今日零点时间
        $zero_time=strtotime(date('Y-m-d',time()));
        $today_all_total = $this->model
            ->where('is_pay',1)
            ->where('pay_time','>',$zero_time)
            ->sum('pay_price');
        $cancel_total = $this->model
            ->where('is_pay',1)
            ->where('pay_time','>',$zero_time)
            ->where('apply_delete',1)
            ->where('is_delete',1)
            ->sum('pay_price');
        //今日店铺除去退款总支付金额
        $today_real_total=$today_all_total-$cancel_total;
        //本周店铺总支付金额
        $beginThisweek = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));
        $week_all_total = $this->model
            ->where('is_pay',1)
            ->where('pay_time','>',$beginThisweek)
            ->sum('pay_price');
        $cancel_week_total = $this->model
            ->where('is_pay',1)
            ->where('pay_time','>',$beginThisweek)
            ->where('apply_delete',1)
            ->where('is_delete',1)
            ->sum('pay_price');
        //今日店铺除去退款总支付金额
        $week_real_total=$week_all_total-$cancel_week_total;

        $result=[
            'today_all_total'=>$today_all_total,
            'today_real_total'=>$today_real_total,
            'today_between_time' => date('Y-m-d H:i:s', $zero_time) .' — '.date('Y-m-d H:i:s', time()),
            'week_all_total'=>$week_all_total,
            'week_real_total'=>$week_real_total,
            'week_between_time' => date('Y-m-d H:i:s', $beginThisweek) .' — '.date('Y-m-d H:i:s', time()),
            'start_date' => date('Y-m-d', strtotime('-1 month', time())),
            'end_date' => date('Y-m-d'),
        ];
        $this->view->assign("result",$result);
        return $this->view->fetch();
    }

    /**
     * Notes:支付金额统计
     * DateTime 2021/6/2 17:46
     * @Author szh
     * @param string $start_date 开始时间
     * @param string $end_date 结束时间
     * @param string $bill_type 统计方式
     */
    public function get_data($start_date = "", $end_date = "", $bill_type = "day")
    {
        //默认一个月内的 按天统计
        if(!$start_date || !$end_date){
            list($start_date, $end_date) = [date('Y-m-d', strtotime('-1 month', time())), date('Y-m-d')];
        }
        // 处理数据结构
        $res = $this->presetList($start_date, $end_date, $bill_type);
        $date_format = $res['date_format'];
        $preset_list = $res['preset_list'];

        if(count($preset_list) > 100){
            return json(['msg' => "数据展示错误，数据跨度不得大于50条"]);
        }
        $start_date = $res['start_date'];//处理一下年、月的类型
        $end_date = $res['end_date'];//处理一下年、月的类型

        $list = $this->model
            ->where('is_pay',1)
            ->where('pay_time','>', strtotime($start_date))
            ->where('pay_time','<', strtotime($end_date))
            ->fieldRaw('FROM_UNIXTIME(pay_time,\''.$date_format.'\') as date, sum(pay_price) as total_price')
            ->group('date')
            ->select();
        $list = collection($list)->toArray();
        $list = array_column($list,'total_price', 'date');

        $real_list = $this->model
            ->where('is_pay',1)
            ->where('is_delete',1)
            ->where('apply_delete',1)
            ->where('pay_time','>', strtotime($start_date))
            ->where('pay_time','<', strtotime($end_date))
            ->fieldRaw('FROM_UNIXTIME(pay_time,\''.$date_format.'\') as date, sum(pay_price) as total_price')
            ->group('date')
            ->select();
        $real_list = collection($real_list)->toArray();
        $real_list = array_column($real_list,'total_price', 'date');

        $category_list = [];
        $total_list = [];
        $real_total_list = [];
        foreach ($preset_list as $k => $v){
            $category_list[] = $v['date'];
            $total_list[] = isset($list[$v['date']])? $list[$v['date']]: 0;
            $real_total_list[] = isset($real_list[$v['date']])? $real_list[$v['date']]: 0;
        }

        return json([
            'category_list' => $category_list,
            'total_list' => $total_list,
            'real_total_list' => $real_total_list,
        ]);
    }

    /**
     * Notes:按类型进行时间格式处理
     * DateTime 2021/6/2 17:45
     * @Author szh
     * @param $start_date
     * @param $end_date
     * @param $bill_type
     * @return array
     */
    protected function presetList($start_date, $end_date, $bill_type)
    {
        switch ($bill_type){
            case "day":
                $date_format = '%Y-%m-%d';
                $temp_date   = strtotime($start_date);
                while ($temp_date <= strtotime($end_date)) {
                    $preset_list[] = [
                        'date' => date('Y-m-d', $temp_date),
                    ];
                    $temp_date = strtotime('+1 day', $temp_date);
                }
                break;
            case "month":
                $start_date = $start_date . "-01";
                $end_date = $end_date . "-" . date('t', strtotime($end_date. "-01"));
                $date_format = '%Y-%m';
                $temp_date   = strtotime($start_date);
                while ($temp_date <= strtotime($end_date)) {
                    $preset_list[] = [
                        'date' => date('Y-m', $temp_date),
                    ];
                    $temp_date = strtotime('+1 month', $temp_date);
                }
                break;
            case "year":
                $start_date = $start_date . "-01-01";
                $end_date = $end_date . "-12-31";
                $date_format = '%Y';
                $temp_date   = strtotime($start_date);
                while ($temp_date <= strtotime($end_date)) {
                    $preset_list[] = [
                        'date' => date('Y', $temp_date),
                    ];
                    $temp_date = strtotime('+1 year', $temp_date);
                }
                break;
            case "hour":
                $date_format = '%Y-%m-%d %H-%i';
                $temp_date   = strtotime($start_date);
                while ($temp_date <= strtotime($end_date)) {
                    $preset_list[] = [
                        'date' => date('Y-m-d H-i', $temp_date),
                    ];
                    $temp_date = strtotime('+1 hours', $temp_date);
                }
                break;
        }
        return [
            'date_format' => $date_format,
            'preset_list' => $preset_list,
            'start_date'  => $start_date,
            'end_date'    => $end_date
        ];
    }
}
